import request from '@/utils/request'
import {ApiResponse,PageData,PageQuery,BaseDetail} from "@/ts/CommonTypes"
import {downloadExcel} from "@/api/ApiUtils";

const urlPrefix = "/api/authResource/";

// 类型定义
export class Resource extends BaseDetail{
    parentId: number = 0;
    children: Resource[];
    groupName: string
    name: string
    iconName: string
    webPath: string
    description: string
    apiSignatureCode: string
    apiNames: string[]
    apiList: string[]
    sort: number
    groupId?: number
    type: 'Menu' | 'Button'
    constructor(init?: Partial<Resource>) {
        super();
        if (init) {
            Object.assign(this, init);
        }
    }
}

export class ResourceQuery extends PageQuery{
    groupId?: number | string;
    constructor(init?: Partial<ResourceQuery>) {
        super();
        if (init) {
            Object.assign(this, init);
        }
    }
}


export function init (data) {
    return request({
        url: urlPrefix + 'init',
        method: 'POST',
        data: data
    })
}

export function list(query:ResourceQuery) {
    return request<ApiResponse<Resource[]>>({
        url:  urlPrefix + 'list',
        method: 'POST',
        data:query
    })
}

export function tree() {
    return request({
        url:  urlPrefix + 'tree',
        method: 'GET'

    })
}

//menuGroupList
export function menuGroupList() {
    return request({
        url:  urlPrefix + 'menuGroupList',
        method: 'GET'

    })
}

export function userMenu() {
    return request({
        url:  urlPrefix + 'userMenu',
        method: 'GET'

    })
}

/**
 * 查询详情
 * @param id
 */
export function getById(id:number) {
    return request<ApiResponse<Resource>>({
        url:  urlPrefix + 'getById?id='+ id ,
        method: 'GET'
    })
}

/**
 * 导出列表
 * @param query
 */
export function exportList(query:ResourceQuery) {
    return request.post(
        urlPrefix + 'export',
        query,
        {responseType:"blob"}
    ).then((res)=>{
        downloadExcel(res,"资源列表.xlsx");
    })
}


/**
 * 保存数据
 * @param type
 */
export function save (data:Resource) {
    return request({
        url: urlPrefix + 'save',
        method: 'POST',
        data: data
    })
}

/**
 * 删除数据
 * @param note
 */
export function deleteById (id:number) {
    if(!id || id <= 0){
        return null;
    }
    return request({
        url: urlPrefix + 'deleteById?id='+ id ,
        method: 'POST'
    })
}
